// pages/match/match.js
var match = require('../../utils/match');
Page({
    uploadImage(){
        var matchid = this.data.matchid;
        var index = this.data.index;
        var image = null;
        var time = new Date();
        var userid = this.data.userid;
        var username = this.data.username;
        var that = this

        wx.chooseMedia({
            count: 1,
            success(res) {
                    console.log(res.tempFiles)
                    var po = res.tempFiles[0].tempFilePath.lastIndexOf(".")
                    var ext = res.tempFiles[0].tempFilePath.slice(po)
                    wx.cloud.uploadFile({
                            cloudPath: matchid + userid + index + ext,
                            filePath: res.tempFiles[0].tempFilePath,
                            success(res) {
                                console.log(res.fileID);
                                image = res.fileID;
                                var database = wx.cloud.database();
                                database.collection("itemScore").add({
                                    data:{
                                        matchid:matchid,
                                        index:index,
                                        image:image,
                                        time:time,
                                        userid:userid,
                                        username:username
                                    }
                                  }).then((res)=>{
                                    if(res){
                                      wx.showToast({
                                        title: '图片上传成功',
                                      })
                                    }else{
                                        wx.showToast({
                                          title: '图片上传失败',
                                        })
                                    }
                                    
                                }).catch((err)=>{
                                    wx.showToast({
                                      title: '图片上传异常',
                                    });
                                    console.log(err);
                                });
                                that.setData({
                                  currentImage:res.fileID
                                });
                            }
                    })
            }
    })
    },
    click:function(e){
        this.setData({
            index : e.currentTarget.id
        })
    },
    daoda(){
        var k = this.data.index;
        this.setData({
          dialogShow:true,
          currentImage:this.data.imageList[k]
        });
    },
    /**
     * 页面的初始数据
     */
    data: {
        times:[],
        username:null,
        itemScore:[],
        currentImage:null,
        userid:null,
        matchid:null,
        title:null,
        imageList:[],
        picnames:[],
        index:0,
        category: 0,
        dialogShow:false,
        id: null,
        matches:match.list,
        little_location: "https://lbs.gtimg.com/visual/miniprogram-plugin/route-planning/assets/route_end.png"
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad(options) {
        //获取用户id
        var that = this;
        wx.getStorage({
            key:"userid",
            success(res){
                that.setData({
                    userid:res.data
                });
                var database = wx.cloud.database();
                database.collection("user").where({
                    _id:res.data.userid
                }).get().then((r)=>{
                    that.setData({
                        username:r.data[0].name
                    });
                })
                database.collection("baoming").where({
                    userid:res.data
                }).get().then((msg)=>{
                    that.setData({
                        matchid:msg.data[0].matchid,
                        title:msg.data[0].title
                    });
                    wx.setNavigationBarTitle({
                      title: msg.data[0].title,
                    });
                    //获取该比赛的对应图片
                    database.collection("match").where({
                        _id:msg.data[0].matchid
                    }).get().then((m)=>{
                        that.setData({
                            imageList:m.data[0].imageList
                        });
                        var picnames = [];
                        for(let i = 0;i < that.data.imageList.length;i++){
                            var loc = that.data.imageList[i].lastIndexOf("/");
                            var picname = that.data.imageList[i].slice(loc + 1);
                            loc = picname.lastIndexOf(".");
                            picname = picname.slice(0,loc);
                            picnames.push(picname);
                        }
                        that.setData({
                            picnames:picnames
                        });
                    });
                    //获取当前已经完成当前比赛的人员数组
                    database.collection("itemScore").where({
                        matchid:that.data.matchid,
                        index:that.data.index
                    }).get().then((s)=>{
                        that.setData({
                            itemScore:s.data
                        });
                        var times = [];
                        for(let i = 0;i < that.data.itemScore;i++){
                            var data = that.data.itemScore[i];
                            var time = data.time;
                            var date = new Date(time);
                            var t = date.toTimeString().substring(0,8);
                            var d = date.toLocaleDateString().substring(0,10);
                            times.push(d + " " + t);
                            that.data.itemScore[i].time = d + " " + t;
                        }
                        that.setData({
                            itemScore:that.data.itemScore
                        });
                    })
                });
            }
        })
    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady() {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow() {

    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide() {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload() {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh() {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom() {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage() {

    }
})